package com.ruoyi.consumer.orders.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.business.rest.ServiceDetailsRestService;
import com.ruoyi.business.shop.api.domain.ServiceDetails;
import com.ruoyi.common.core.utils.QRCodeGenerator;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.consumer.orders.domain.Orders;
import com.ruoyi.consumer.orders.mapper.OrdersMapper;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.consumer.orders.service.IOrdersService;

import javax.annotation.Resource;

/**
 * 订单Service业务层处理
 * 
 * @author fkk
 * @date 2025-02-21
 */
@Service
public class OrdersServiceImpl implements IOrdersService 
{
    @Autowired
    private OrdersMapper ordersMapper;

    // 根据token获取用户信息
    @Resource
    private TokenService tokenService;

    // 生成二维码
    @Resource
    private QRCodeGenerator qrCodeGenerator;

    // 服务详情
    @Resource
    private ServiceDetailsRestService serviceDetailsRestService;

    /**
     * 查询订单
     * 
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public Orders selectOrdersById(Integer id)
    {
        return ordersMapper.selectOrdersById(id);
    }

    /**
     * 查询订单列表
     * 
     * @param orders 订单
     * @return 订单
     */
    @Override
    public List<Orders> selectOrdersList(Orders orders)
    {
        List<Orders> list = ordersMapper.selectOrdersList(orders);
        System.out.println("*********************************************");
        list.forEach(System.out::println);
        System.out.println("*********************************************");
        return list;
    }

    /**
     * 新增订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int insertOrders(Orders orders, String token) throws InterruptedException {
        // 通过TokenService获取当前登录者的信息并将用户id存入orders
        LoginUser loginUser = tokenService.getLoginUser(token);
        System.out.println("loginUser = " + loginUser);
        orders.setUid(Math.toIntExact(loginUser.getUserid()));

        // 使用UUID生成核销码并存入orders
        String recordUUID = String.valueOf(UUID.fastUUID());
        orders.setVerificationCode(recordUUID);

        // 根据核销码生成二维码并存入orders
        String codeImg = qrCodeGenerator.generateQRCode(recordUUID);
        orders.setVerificationImg(codeImg);
        System.out.println("codeImg = " + codeImg);
        System.out.println("recordUUID = " + recordUUID);

        // 计算实付金额并存入orders
        // 根据服务详情id
        System.out.println("orders.getServiceId() = " + orders.getServiceId());
        AjaxResult ajaxResult = serviceDetailsRestService.getInfo(orders.getServiceId());
        System.out.println("ajaxResult = " + ajaxResult);
        if (ajaxResult != null) {
            Map<String,Object> data = (Map<String, Object>) ajaxResult.get("data");
            orders.setServiceTypeId((Integer) data.get("typeId"));
            System.out.println("data.get(\"typeId\") = " + data.get("typeId"));
        }
        System.out.println("orders = " + orders);
        return ordersMapper.insertOrders(orders);
//        return 0;
    }

    /**
     * 修改订单
     * 
     * @param orders 订单
     * @return 结果
     */
    @Override
    public int updateOrders(Orders orders)
    {
        return ordersMapper.updateOrders(orders);
    }

    /**
     * 批量删除订单
     * 
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersByIds(Integer[] ids)
    {
        return ordersMapper.deleteOrdersByIds(ids);
    }

    /**
     * 删除订单信息
     * 
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteOrdersById(Integer id)
    {
        return ordersMapper.deleteOrdersById(id);
    }
}
